﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class DataBaseHelper
    {
        private readonly string sqlCommandString = "select * from school";

        private readonly string connectionString = "server=.;database=dbtest;uid=sa;pwd=123456";

        public static void PrintUser()
        {
            string connectionString = "server=.;database=dbtest;uid=sa;pwd=123456";

            SqlConnection sqlConnection = new SqlConnection(connectionString);

            sqlConnection.Open();

            var sqlCommandString = "select * from school";

            SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection);

            var sdr = sqlCommand.ExecuteReader();
            Console.WriteLine("{0}\t{1}\t{2}", "schoolId", "schoolName", "createTime");
            while (sdr.Read())
            {
                Console.WriteLine("{0}\t{1}\t{2}", sdr["schoolId"], sdr["schoolName"], sdr["createTime"]);
            }
            sqlConnection.Close();
        }


        public void PrintUserInfor()
        {
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommandString, connectionString);

            DataTable dataTable = new DataTable();

            dataAdapter.Fill(dataTable);

            Console.WriteLine();

            Console.WriteLine();

            Console.WriteLine();

            Console.WriteLine("{0}\t{1}\t{2}", "schoolId", "schoolName", "createTime");

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                Console.WriteLine("{0}\t{1}\t{2}", dataTable.Rows[i]["schoolId"], dataTable.Rows[i]["schoolName"], dataTable.Rows[i]["createTime"]);
            }

        }
    }
}
